package com.slipper.system.codeless.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.DataIndexDao;
import com.slipper.system.codeless.bo.DataIndexBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class DataIndexService {
    @Resource
    private DataIndexDao dataIndexDao;
    
    public DataIndexBO queryBy(String id) {
        return dataIndexDao.queryBy(id);
    }
    
    public List<DataIndexBO> queryList(DataIndexBO bo) {
        return dataIndexDao.queryList(bo);
    }

    public PageInfo<DataIndexBO> queryPage(DataIndexBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dataIndexDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(DataIndexBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return dataIndexDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return dataIndexDao.insert(bo);
        }
    }

    @WriteTransactional
    public int save(String modelId, List<DataIndexBO> list, String employeeId) {
        if (Tool.isBlank(modelId)) {
            return 0;
        }

        // 保存/更新数据
        int i = 0;
        for (DataIndexBO index : list) {
            index.setModelId(modelId);
            index.setOrdinal(++i);
            if (Tool.isBlank(index.getId())) {
                index.setId(UUID.getUUID());
                index.setUpdateId(employeeId);
                index.setCreateId(employeeId);
                index.setUpdateTime(System.currentTimeMillis());
                index.setCreateTime(System.currentTimeMillis());
                dataIndexDao.insert(index);
            } else {
                index.setUpdateId(employeeId);
                index.setUpdateTime(System.currentTimeMillis());
                dataIndexDao.update(index);
            }
        }

        // 删除被删除的数据
        DataIndexBO qo = new DataIndexBO();
        qo.setModelId(modelId);
        dataIndexDao.queryList(qo).stream()
                .filter(index -> list.stream().noneMatch(temp -> index.getId().equals(temp.getId())))
                .forEach(this::delete);
        return 1;
    }
    
    @WriteTransactional
    public int delete(DataIndexBO bo) {
        return dataIndexDao.delete(bo);
    }

    @WriteTransactional
    public void deleteByModelId(String modelId) {
        if (Tool.isBlank(modelId)) return;
        DataIndexBO qo = new DataIndexBO();
        qo.setModelId(modelId);
        this.dataIndexDao.queryList(qo).forEach(this::delete);
    }
}
